<?php
// +----------------------------------------------------------------------
// | ThinkPHP [ WE CAN DO IT JUST THINK ]
// +----------------------------------------------------------------------
// | Copyright (c) 2006-2016 http://thinkphp.cn All rights reserved.
// +----------------------------------------------------------------------
// | Licensed ( http://www.apache.org/licenses/LICENSE-2.0 )
// +----------------------------------------------------------------------
// | Author: 流年 <liu21st@gmail.com>
// +----------------------------------------------------------------------

// 应用公共文件

/**
 * 模拟tab产生空格
 * @param int $step
 * @param string $string
 * @param int $size
 * @return string
 */

use  think\Db;

function tab($step = 1, $string = ' ', $size = 4)
{
    return str_repeat($string, $size * $step);
}


/**
 * 检测验证码
 * @param  integer $id 验证码ID
 * @return boolean     检测结果
 * @author 麦当苗儿 <zuojiazi@vip.qq.com>
 */
function check_verify($code, $id = 1)
{
    $verify = new \Think\Verify();
    return $verify->check($code, $id);
}


function str_mobile($str)
{
    if ($str) {
        $str = substr_replace($str, '****', 3, 4);
        return $str;
    } else {
        return '';
    }

}

function str_id_card($str)
{
    if ($str) {
        $str = substr_replace($str, '***********', 3, 11);
        return $str;
    } else {
        return '';
    }
}

function str_name($str)
{
    $str = mb_substr($str, 0, 1);
    if ($str) {
        return $str . "**";
    } else {
        return '';
    }
}

//新手任务日常任务

function dailyTasks($url, $evt, $uid, $income)
{
    $time = time();
    $data = [
        'income' => $income,
        'evt' => $evt,
        'rtype' => 20,
        'jobid' => 0,
        'uid' => $uid,
        'timestamp' => $time,
        'sign' => md5($time . $uid . config('urlsKey')),
    ];
    return curl_posts($url, $data);
}


//新手注册
function newreg($url, $evt, $uid, $income)
{
    $time = time();
    $data = [
        'income' => $income,
        'evt' => $evt,
        'rtype' => 30,
        'jobid' => 0,
        'uid' => $uid,
        'timestamp' => $time,
        'sign' => md5($time . $uid . config('urlsKey')),
    ];
    return curl_posts($url, $data);
}


//增加签到任务金额
function signLog($url, $uid, $dayNum)
{
    $income = 2;
    //额外奖励数
    $incomeNum = [
        3 => 20,
        7 => 40,
        15 => 60,
        30 => 120,
    ];
    foreach ($incomeNum as $k => $val) {
        if ($dayNum == $k) {
            $income += $val;
            break;
        }
    }
    $time = time();
    $data = [
        'income' => $income,
        'evt' => '签到',
        'rtype' => 1,
        'jobid' => 0,
        'uid' => $uid,
        'timestamp' => $time,
        'sign' => md5($time . $uid . config('urlsKey')),
    ];
    return curl_posts($url, $data);
}

//增加悬赏任务任务金额
function addaccountTask($url, $uid, $income, $jobid)
{
    $time = time();
    $data = [
        'income' => $income,
        'evt' => '悬赏任务',
        'rtype' => 3,
        'jobid' => $jobid,
        'uid' => $uid,
        'timestamp' => $time,
        'sign' => md5($time . $uid . config('urlsKey')),
    ];
    return curl_posts($url, $data);
}

//审核发布任务失败返回金额
function auditTaskFail($url, $title, $uid, $income, $jobid)
{
    $time = time();
    $data = [
        'income' => $income,
        'evt' => '悬赏任务"' . $title . '"发布失败',
        'rtype' => 50,
        'jobid' => $jobid,
        'uid' => $uid,
        'timestamp' => $time,
        'sign' => md5($time . $uid . config('urlsKey')),
    ];
    return curl_posts($url, $data);
}


//徒弟提现师傅加钱
function masterAccount($url, $uid, $income)
{

    $time = time();
    $data = [
        'income' => $income,
        'evt' => '徒弟提现',
        'rtype' => 9,
        'jobid' => 0,
        'uid' => $uid,
        'timestamp' => $time,
        'sign' => md5($time . $uid . config('urlsKey')),
    ];
    return curl_posts($url, $data);
}

//增加收徒任务奖励
function inviteRewards($url, $uid)
{
    $income = 100;
    $time = time();
    $data = [
        'income' => $income,
        'evt' => '收徒',
        'rtype' => 4,
        'jobid' => 0,
        'uid' => $uid,
        'timestamp' => $time,
        'sign' => md5($time . $uid . config('urlsKey')),
    ];
    return curl_posts($url, $data);
}

//提现记录
function withdrawal($url, $uid, $income, $aliid, $aliname)
{
    $time = time();
    $data = [
        'ta' => $income,
        'uid' => $uid,
        'aliid' => $aliid,
        'aliname' => $aliname,
        'timestamp' => $time,
        'sign' => md5($time . $uid . config('urlsKey'))
    ];
    return curl_postsss($url, $data);
}


//发布任务减少金额
function minusaccount($info)
{
    $time = time();
    $data = [
        'income' => $info['income'],
        'evt' => $info['evt'],
        'rtype' => 1000,
        'jobid' => $info['jobid'],
        'uid' => $info['uid'],
        'timestamp' => $time,
        'sign' => md5($time . $info['uid'] . config('urlsKey')),
    ];
    return curl_posts($info['url'], $data);
}

function curl_posts($url, $data)
{
    $ch = curl_init();
    curl_setopt($ch, CURLOPT_URL, $url);
    curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);
    curl_setopt($ch, CURLOPT_POST, 1);
    curl_setopt($ch, CURLOPT_POSTFIELDS, $data);
    curl_setopt($ch, CURLOPT_TIMEOUT, 10);          //单位 秒，也可以使用
    curl_setopt($ch, CURLOPT_NOSIGNAL, 1);     //注意，毫秒超时一定要设置这个
    curl_setopt($ch, CURLOPT_TIMEOUT_MS, 3000); //超时毫秒，cURL 7.16.2中被加入。从PHP 5.2.3起可使用
    $output = curl_exec($ch);
    curl_close($ch);
    $result = object_arrays(json_decode($output));

    if ($result['errcode'] == 0) {
        return true;
    }
    return false;
}

function curl_postsss($url, $data)
{
    $ch = curl_init();
    curl_setopt($ch, CURLOPT_URL, $url);
    curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);
    curl_setopt($ch, CURLOPT_POST, 1);
    curl_setopt($ch, CURLOPT_POSTFIELDS, $data);
    curl_setopt($ch, CURLOPT_TIMEOUT, 10);          //单位 秒，也可以使用
    curl_setopt($ch, CURLOPT_NOSIGNAL, 1);     //注意，毫秒超时一定要设置这个
    curl_setopt($ch, CURLOPT_TIMEOUT_MS, 3000); //超时毫秒，cURL 7.16.2中被加入。从PHP 5.2.3起可使用
    $output = curl_exec($ch);

    curl_close($ch);
    $result = object_arrays(json_decode($output));
    return $result;
}

//获取账户余额
function baseDailyTasks($url, $uid)
{
    $time = time();
    $data = [
        'uid' => $uid,
        'timestamp' => $time,
        'sign' => md5($time . $uid . config('urlsKey')),
    ];
    $userEarn = curl_post($url, $data);

    return $userEarn;
}


//获取账户余额
function userBalance($url, $uid)
{
    $time = time();
    $data = [
        'uid' => $uid,
        'timestamp' => $time,
        'sign' => md5($time . $uid . config('urlsKey')),
    ];
    $userEarn = curl_post($url, $data);
    foreach ($userEarn as $k => $val) {
        if ($val > 0) {
            $userEarn[$k] = sprintf("%.2f", ($val / 100));
        }
    }
    return $userEarn;
}

//获取首页悬赏任务列表
function joblist($url, $uid)
{
    $time = time();
    $data = [
        'uid' => $uid,
        'timestamp' => $time,
        'sign' => md5($time . $uid . config('urlsKey')),
    ];
    $userEarn = curl_postbc($url, $data);

    return $userEarn;
}

//获取正在进行的首页悬赏任务
function getjob($url, $uid, $fid)
{
    $time = time();
    $data = [
        'uid' => $uid,
        'timestamp' => $time,
        'sign' => md5($time . $uid . config('urlsKey')),
        'fid' => $fid
    ];
    $userEarn = curl_postgetjob($url, $data);

    return $userEarn;
}

//收入明细
function incomelog($url, $uid)
{
    $time = time();
    $data = [
        'uid' => $uid,
        'timestamp' => $time,
        'sign' => md5($time . $uid . config('urlsKey')),
    ];
    return curl_post($url, $data);
}

//取消 放弃 未完成 屏蔽任务
function userjobstatus($url, $uid, $tid, $sta)
{
    $time = time();
    $data = [
        'uid' => $uid,
        'timestamp' => $time,
        'sign' => md5($time . $uid . config('urlsKey')),
        'sta' => $sta,
        'tid' => $tid
    ];
    return curl_postgetjob($url, $data);
}

//清楚任务缓存
function updateredis($url, $uid)
{
    $time = time();
    $data = [
        'uid' => $uid,
        'timestamp' => $time,
        'sign' => md5($time . $uid . config('urlsKey'))
    ];
    return curl_postgetjob($url, $data);
}

function broadcast($url, $uid)
{
    $time = time();
    $data = [
        'uid' => $uid,
        'timestamp' => $time,
        'sign' => md5($time . $uid . config('urlsKey'))
    ];
    return curl_postgetjob($url, $data);
}

function surplus($url, $uid, $taskid)
{
    $time = time();
    $data = [
        'uid' => $uid,
        'taskid' => $taskid,
        'timestamp' => $time,
        'sign' => md5($time . $uid . config('urlsKey'))
    ];
    return curl_postgetjob($url, $data);
}

function curl_postgetjob($url, $data)
{
    $ch = curl_init();
    curl_setopt($ch, CURLOPT_URL, $url);
    curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);
    curl_setopt($ch, CURLOPT_POST, 1);
    curl_setopt($ch, CURLOPT_POSTFIELDS, $data);
    curl_setopt($ch, CURLOPT_TIMEOUT, 10);          //单位 秒，也可以使用
    curl_setopt($ch, CURLOPT_NOSIGNAL, 1);     //注意，毫秒超时一定要设置这个
    curl_setopt($ch, CURLOPT_TIMEOUT_MS, 3000); //超时毫秒，cURL 7.16.2中被加入。从PHP 5.2.3起可使用
    $output = curl_exec($ch);
    curl_close($ch);
    $result = object_arrays(json_decode($output));
    return $result;;
}

function curl_postbc($url, $data)
{
    $ch = curl_init();
    curl_setopt($ch, CURLOPT_URL, $url);
    curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);
    curl_setopt($ch, CURLOPT_POST, 1);
    curl_setopt($ch, CURLOPT_POSTFIELDS, $data);
    curl_setopt($ch, CURLOPT_TIMEOUT, 10);          //单位 秒，也可以使用
    curl_setopt($ch, CURLOPT_NOSIGNAL, 1);     //注意，毫秒超时一定要设置这个
    curl_setopt($ch, CURLOPT_TIMEOUT_MS, 3000); //超时毫秒，cURL 7.16.2中被加入。从PHP 5.2.3起可使用
    $output = curl_exec($ch);
    curl_close($ch);
   
    if($output){
        $result = object_arrays(json_decode($output));
        if ($result['errcode'] == 0) {
            return json_decode($result['data'], true);
        }
    }
    return [];
}

function curl_post($url, $data)
{
    $ch = curl_init();
    curl_setopt($ch, CURLOPT_URL, $url);
    curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);
    curl_setopt($ch, CURLOPT_POST, 1);
    curl_setopt($ch, CURLOPT_POSTFIELDS, $data);
    curl_setopt($ch, CURLOPT_TIMEOUT, 10);          //单位 秒，也可以使用
    curl_setopt($ch, CURLOPT_NOSIGNAL, 1);     //注意，毫秒超时一定要设置这个
    curl_setopt($ch, CURLOPT_TIMEOUT_MS, 3000); //超时毫秒，cURL 7.16.2中被加入。从PHP 5.2.3起可使用
    $output = curl_exec($ch);
    curl_close($ch);
    $result = object_arrays(json_decode($output));
    if ($result['errcode'] == 0) {
        return $result['data'];
    }
    return false;
}


//提现
function withdrawalLog($url, $uid)
{
    $time = time();
    $data = [
        'uid' => $uid,
        'timestamp' => $time,
        'sign' => md5($time . $uid . config('urlsKey')),
    ];
    return curl_posttt($url, $data);
}


function curl_posttt($url, $data)
{
    $ch = curl_init();
    curl_setopt($ch, CURLOPT_URL, $url);
    curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);
    curl_setopt($ch, CURLOPT_POST, 1);
    curl_setopt($ch, CURLOPT_POSTFIELDS, $data);
    curl_setopt($ch, CURLOPT_TIMEOUT, 10);          //单位 秒，也可以使用
    curl_setopt($ch, CURLOPT_NOSIGNAL, 1);     //注意，毫秒超时一定要设置这个
    curl_setopt($ch, CURLOPT_TIMEOUT_MS, 3000); //超时毫秒，cURL 7.16.2中被加入。从PHP 5.2.3起可使用
    $output = curl_exec($ch);
    curl_close($ch);
    $result = object_arrays(json_decode($output));
    if ($result['errcode'] == 0) {
        return $result;
    }
    return false;
}


/**
 *图片上传api
 */
function getImgUrl($data, $filenames, $type = '')
{
    $str = explode(",", $data);//base64截取逗号以后的
    if ($str[0] != "data:image/jpeg;base64") {
        return $data;
    }
    //$str = explode(",", $data);//base64截取逗号以后的
    $content = array(
        'imgBase64' => $str[(count($str) - 1)],//写入的数据
        'fileName' => uniqid(),//文件名
        'oss_path' => $filenames//文件
    );

    //图片base64编码
    $imgInfo = base64_decode($content['imgBase64']);
    //图片名称
    $fileName = $content['fileName'];
    $pathRead = $fileName . '.jpg';
    $path = '../public/static/' . $pathRead;
    //返回的是字节数
    $result = file_put_contents($path, $imgInfo);
    if ($result) {
        vendor('txcos/vendor/autoload');
        $secretId = "AKIDSpysYyE3kRta2Fsk4VilfJgEcmhsZJbb"; //"云 API 密钥 SecretId";
        $secretKey = "uTlbAUZdRyVnnZrVx8uOgaGfw4nth17y"; //"云 API 密钥 SecretKey";
        $region = "ap-guangzhou"; //设置一个默认的存储桶地域
        $cosClient = new Qcloud\Cos\Client(
            array(
                'region' => $region,
                'schema' => 'https', //协议头部，默认为http
                'credentials' => array(
                    'secretId' => $secretId,
                    'secretKey' => $secretKey)));
        ### 上传文件流
        $file = fopen($path, 'rb');
        if ($file) {
            $result = $cosClient->Upload(
                $bucket = 'xslm-1252482422',
                $key = $pathRead,
                $body = $file);
        }
        $res = object_arrays($result);
        unlink($path);
        if ($res) {
            $url = '';
            foreach ($res as $v) {
                $url = $v['Location'];
            }
            return 'https://' . $url;
        }
        return false;
    }
}


/**
 *图片上传api
 */
function getImgUrls($path, $pathRead)
{
    vendor('txcos/vendor/autoload');
    $secretId = "AKIDSpysYyE3kRta2Fsk4VilfJgEcmhsZJbb"; //"云 API 密钥 SecretId";
    $secretKey = "uTlbAUZdRyVnnZrVx8uOgaGfw4nth17y"; //"云 API 密钥 SecretKey";
    $region = "ap-guangzhou"; //设置一个默认的存储桶地域
    $cosClient = new Qcloud\Cos\Client(
        array(
            'region' => $region,
            'schema' => 'https', //协议头部，默认为http
            'credentials' => array(
                'secretId' => $secretId,
                'secretKey' => $secretKey)));
    ### 上传文件流
    $file = fopen($path, 'rb');
    if ($file) {
        $result = $cosClient->Upload(
            $bucket = 'xslm-1252482422',
            $key = $pathRead,
            $body = $file);
    }
    $res = object_arrays($result);
    unlink($path);
    if ($res) {
        $url = '';
        foreach ($res as $v) {
            $url = $v['Location'];
        }
        return 'https://' . $url;
    }
    return false;
}


function object_arrays($array)
{
    if (is_object($array)) {
        $array = (array)$array;
    }
    if (is_array($array)) {
        foreach ($array as $key => $value) {
            $array[$key] = object_arrays($value);
        }
    }
    return $array;
}

function curl_post_data($url, $data)
{
    $ch = curl_init();
    curl_setopt($ch, CURLOPT_URL, $url);
    curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);
    curl_setopt($ch, CURLOPT_POST, false);
    curl_setopt($ch, CURLOPT_HTTPHEADER, array_merge(array('Content-Type:application/json'), $aHeader));
    curl_setopt($ch, CURLOPT_TIMEOUT, $timeOut);          //单位 秒，也可以使用
    $data = curl_exec($ch);
    if (curl_errno($ch)) {
        $data = curl_error($ch);
    }
    curl_close($ch);
    return $data;
}

function inviteCode()
{
    static $inviteCode = 0;
    $inviteCode = mt_rand(1000000, 9999999);
    $id = Db::name('user')->where(['invite_code' => $inviteCode])->value('id');
    if ($id) {
        inviteCode();
    } else {
        return $inviteCode;
    }
}


function mkdirs($dir, $mode = 0777)
{
    if (is_dir($dir) || @mkdir($dir, $mode)) return TRUE;
    return @mkdir($dir, $mode);
}


//图片上传到oss
function ossFile($list, $url)
{
    $ch = curl_init();
    curl_setopt($ch, CURLOPT_URL, $url);
    curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);
    curl_setopt($ch, CURLOPT_POST, 1);
    curl_setopt($ch, CURLOPT_POSTFIELDS, $list);
    $output = curl_exec($ch);
    curl_close($ch);
    $result = json_decode($output, true);
    return $result['url'];
}


function get_mobile_str($str)
{
    if ($str) {
        $str = substr_replace($str, '********', 3, 8);
        return $str;
    } else {
        return '';
    }
}

function str_ReplaceCn($str, $rstr = '*')
{
    $replace = '';
    $length = mb_strlen($str, 'utf-8');
    for ($i = 0; $i < $length - 1; $i++) {
        $replace .= $rstr;
    }
    $last = mb_substr($str, -1, 1, 'utf-8');
    return $replace . $last;
}

/*身份证脱敏*/
function strCardReplace($str)
{
    if (!$str) {
        return '';
    }
    $stt = substr_replace($str, '****', 12, 4);
    $last = substr_replace($stt, '*', 17, 1);
    return $last;
}




